import { initState } from "./instance/state.js";
import Watcher from "./observer/watcher";
import { mergeOptions } from "./utils/index.js";

export function initMixin(Vue) {
  Vue.prototype._init = function (options) {
    const vm = this;
    // this.$options = options
    this.$options = mergeOptions(this.constructor.options, options);

    callHook(vm, "beforeCreate");
    initState(vm);
    callHook(vm, "created");
  };
  //挂载
  Vue.prototype.$mount = function (options) {
    console.log("组件挂载了------");
    const watcher = new Watcher(
      this,
      () => {
        // this.num;
        // this.name;
        // this.attrs;
        // this.myStr;
        // this.arr1;
        // this.obj;
        // this.customArr;
        debugger
        this.name
        this.fullname
        this.fullname
        this.fullname
      },
      null,
      null,
      true
    );

    // console.log(watcher);
  };

  Vue.prototype.$watch = function (exprOrFn, handler, options) {
    options = options || {};
    options.user = true;
    new Watcher(this, exprOrFn, handler, options); //自定义watcher
  };
}

function callHook(vm, hook) {
  const handlers = vm.$options[hook];
  if (handlers) {
    handlers.forEach((handler) => handler.call(vm));
  }
}
